<?php
require_once __DIR__ . '/vendor/autoload.php';

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class Helper_liveroom_sendsms {
    
    /**
     * 
     * @param varchar $mobile 手机号码
     * @param varchar $smssign  签名---观古网拍
     * @param varchar $templateid  模板id
     * @param array $param  变量数组  array('goodsname'=>'18380132757','money'=>100)
     *SMS_182535817--通知买家付款---- 尊敬的顾客，您好！恭喜您拍到宝贝${goodsname}，请24小时内付款，待支付金额${money}
     */
    static function sendsms($mobile,$smssign,$templateid,$param){
        $acct = Acct::find('id = 1')->getOne();
        if(empty($acct->aliaccessid) || empty($acct->aliaccesskey)){
            return array('code'=>2,'msg'=>'请先完善阿里云配置');
        }
        AlibabaCloud::accessKeyClient($acct->aliaccessid, $acct->aliaccesskey)
        ->regionId('cn-hangzhou')
        ->asDefaultClient();
       try {
        $result = AlibabaCloud::rpc()
        ->product('Dysmsapi')
        // ->scheme('https') // https | http
        ->version('2017-05-25')
        ->action('SendSms')
        ->method('POST')
        ->host('dysmsapi.aliyuncs.com')
        ->options([
            'query' => [
                'RegionId' => "cn-hangzhou",
                'PhoneNumbers' => $mobile,
                'SignName' => $smssign,
                'TemplateCode' => $templateid,
                'TemplateParam'=>json_encode($param),
            ],
        ])->request();
          $res = $result->toArray();
          return array('code'=>0,'msg'=>$res);
        } catch (ClientException $e) {
            $res = $e->getErrorMessage();
            return array('code'=>1,'msg'=>$res);
        } catch (ServerException $e) {
            $res = $e->getErrorMessage();
            return array('code'=>1,'msg'=>$res);
        }
        
    }
}

    ?>